//
//  SSOrginalView.swift
//  weibo
//
//  Created by 邵帅 on 16/6/27.
//  Copyright © 2016年 groupNineObject. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit


let HomeCellMargin: CGFloat = 10

class SSOrginalView: UIView {
    
    var bottomConstraint: Constraint?

    var orginalViewModel: SSStatusCellModel?{
        
        didSet{
            
            photoImageView.sd_setImageWithURL(orginalViewModel?.photoUrl)
            nameLabel.text = orginalViewModel?.name
            levelImageView.image = orginalViewModel?.levelImage
            verfityImageView.image = orginalViewModel?.verifyImage
//            contentLabel.text = orginalViewModel?.content
            
            contentLabel.attributedText = orginalViewModel?.attributeText
            
            sourceLabel.text = orginalViewModel?.sourceString
            timeLabel.text = orginalViewModel?.createtimeString
            
            
            bottomConstraint?.uninstall()
            
            if orginalViewModel?.model?.picture_urls?.count > 0 {
                
                pictureView.hidden = false
                
                self.snp_updateConstraints(closure: { (make) in
                    
                  self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                    
                    pictureView.pic_urls = orginalViewModel?.model?.picture_urls
                })
            }else {
                
                pictureView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) in
                    
                   self.bottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
            }
            
        }
        
    }
    
    // 第一步 -- 重写构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 第二步 -- 定义一个方法,来实现 视图的添加和布局
    private func setupUI(){
        
        addSubview(photoImageView)
        addSubview(levelImageView)
        addSubview(verfityImageView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        // 设置约束 -- 一定要固定死一个控件,然后其他的控件都是参照与这个控件
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(HomeCellMargin)
            make.top.equalTo(self.snp_top).offset(HomeCellMargin)
            
            make.width.equalTo(40)
            make.height.equalTo(40)
            // 怕由于snapkit的版本原因不起作用
            //            make.height.width.equalTo(40)
        }
        
        verfityImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(photoImageView.snp_right)
            make.centerY.equalTo(photoImageView.snp_bottom)
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(HomeCellMargin)
            make.top.equalTo(photoImageView.snp_top)
        }
        
        levelImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel.snp_centerY)
            make.left.equalTo(nameLabel.snp_right).offset(HomeCellMargin)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_left)
            make.top.equalTo(nameLabel.snp_bottom).offset(0.5 * HomeCellMargin)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(timeLabel.snp_centerY)
            make.left.equalTo(timeLabel.snp_right).offset(HomeCellMargin)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(photoImageView.snp_left)
            make.top.equalTo(photoImageView.snp_bottom).offset(HomeCellMargin)
            //            make.right.equalTo(self.snp_right).offset(HomeCellMargin)
            // 设置width -- 为什么建议设置width
            make.width.equalTo(UIScreen.mainScreen().bounds.width - 2 * HomeCellMargin)
            
            // 设置底部
            make.bottom.equalTo(pictureView.snp_top).offset(-8)
            
        }
        
        pictureView.snp_makeConstraints { (make) in
            
            make.left.equalTo(self.snp_left).offset(10)
            
            make.width.height.equalTo(100)
        }
 
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
        }
    }
    
    // 第三步: 懒加载控件
    
    ///  头像
    private lazy var photoImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_default_big")
        
        return img
        
    }()
    /// 昵称
    private lazy var nameLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "昵称"
        
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(17)
        
        
        
        return label
        
    }()
    ///时间
    private lazy var timeLabel: UILabel = {
        
        let label = UILabel()
        
        label.textColor = UIColor.orangeColor()
        
        label.text = "时间"
        
        label.font = UIFont.systemFontOfSize(14)
        
        return label
        
    }()
    /// 来源
    private lazy var sourceLabel: UILabel = {
        
        let label = UILabel()
        
        label.textColor = UIColor.orangeColor()
        
        label.text = "来源"
        
        label.font = UIFont.systemFontOfSize(14)
        
        return label
        
    }()
    
    /// 等级
    private lazy var levelImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "common_icon_membership_level1")
        
        return img
    }()
    
    /// 认证
    private lazy var verfityImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_vip")
        
        return img
    }()
    /// 微博内容
    private lazy var contentLabel: UILabel = {
        
        let lable = UILabel()
        
        
        lable.text = "fadshjkfaldshfjkadhslkfhadskl"
        
        lable.font = UIFont.systemFontOfSize(16)
        
        lable.numberOfLines = 0
        
        lable.textColor = UIColor.blackColor()
        
        return lable
        
    }()
    
    private lazy var pictureView: SSPictureView = SSPictureView()
}

    

